<?php

declare(strict_types=1);

use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\RequestHandlerInterface;
use DI\Attribute\Inject;
use Slim\App;
class ExampleMiddlewareV2_1
{
    // 如何通过DI 声明式注入Slim\App到其属性
    // 这里的$app是通过DI注入的
    /**
     * @Inject
     * @var App
     */
    public App $app;


    public function __construct(App $app)
    {

        $this->app = $app;
    }


    public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        // Handle the incoming request
        // ...
        $flag = true;
        if ($flag) {
            $response = $this->app->getResponseFactory()->createResponse()->withHeader("X-Middleware", "true");
            return $response;
        }
        error_log("Before middleware");
        // Invoke the next middleware and return response
        $response = $handler->handle($request);
        // Handle the outgoing response
        // ...
        error_log("after middleware");
        return $response;
    }
}